import re

def clean_str(string, TREC=False):
    """
    Tokenization/string cleaning for all datasets except for SST.
    Every dataset is lower cased except for TREC
    """
    string = re.sub(r"[^A-Za-z0-9(),!?\'\`]", " ", string)
    string = re.sub(r"\'s", " \'s", string)
    string = re.sub(r"\'ve", " \'ve", string)
    string = re.sub(r"n\'t", " n\'t", string)
    string = re.sub(r"\'re", " \'re", string)
    string = re.sub(r"\'d", " \'d", string)
    string = re.sub(r"\'ll", " \'ll", string)
    string = re.sub(r",", " , ", string)
    string = re.sub(r"!", " ! ", string)
    string = re.sub(r"\(", " \( ", string)
    string = re.sub(r"\)", " \) ", string)
    string = re.sub(r"\?", " \? ", string)
    string = re.sub(r"\s{2,}", " ", string)
    return string.strip() if TREC else string.strip().lower()

def strQ2B(ustring):
    # 全角，半角符号转换
    ss = []
    for s in ustring:
        rstring = ""
        for uchar in s :
            inside_code = ord(uchar)
            if inside_code == 12288: # 全角空格直接转换
                inside_code = 32
            elif (inside_code >= 65281
                    and inside_code <= 65374): # 全角字符(除空格)根据关系转化
                inside_code -= 65248
            rstring += chr(inside_code)
        ss.append(rstring)
    return "".join(ss)

# 停用词
# data['text'] = data['text'].apply(lambda x: " ".join([w for w in x.split()
#                                                       if w not in self.stopWords and w != '']))



